package com.yycx.module.bus.provider.handler.haiying.deposit;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.interceptor.SaveInterceptor;
import com.yycx.common.mybatis.query.CriteriaSave;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.OrderNoUtil;
import com.yycx.module.bus.client.entity.BusCustom;
import com.yycx.module.bus.client.entity.BusPayment;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.bus.provider.service.BusCustomService;
import com.yycx.module.system.client.entity.SysCompany;
import com.yycx.module.system.provider.service.SysCompanyService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 质押金收款保存扩展器
 *
 * @author zyf
 */
@Component
public class HyDepositPaymentSaveHandler implements SaveInterceptor<BusPayment> {

    @Resource
    private OrderNoUtil orderNoUtil;

    @Resource
    private SysCompanyService companyService;

    @Resource
    private BusCustomService customService;


    @Override
    public void prepare(CriteriaSave cs, EntityMap entityMap, BusPayment payment) {
        Long companyId = OpenHelper.getCompanyId();
        payment.setPaymentNo(orderNoUtil.getCode("S"));
        payment.setTitle("保证金收款");
        String customCode = cs.getParams("customCode");
        if (FlymeUtils.isNotEmpty(customCode)) {
            BusCustom custom = customService.selectByCustomCode(customCode, companyId);
            SysCompany company = companyService.getById(companyId);
            if (FlymeUtils.isEmpty(custom)) {
                custom = customService.syncBusCustom(company.getBusinessScope(), customCode, companyId);
            }
            if (FlymeUtils.isNotEmpty(custom)) {
                payment.setCustomId(custom.getCustomId().toString());
            }
        }
        payment.setAmountType(AmountTypeEnum.DepositAmount.getCode());
        payment.setOrganizationId(OpenHelper.getOrganizationId());
        payment.setCompanyId(OpenHelper.getCompanyId());
        payment.setUserId(OpenHelper.getUserId());
        payment.setNickName(OpenHelper.getNickName());
        payment.setPaymentState(CommonConstants.INT_0);
    }
}
